import sys
from pandac.PandaModules import *
from direct.interval.IntervalGlobal import *


MAX_PICTURE_SIZE = 10


class Pr3daNodePath():
  def __init__(self, npName='np'):
    self.node_path = NodePath(npName)

  def getNodePath(self):
    return self.node_path


class Picture(Pr3daNodePath):
  def __init__(self, url, loadImageCallback):
    Pr3daNodePath.__init__(self, 'picture')
    self.url = url
    self.loadImage = loadImageCallback

    self._image = None
    self._texture = None
    
    self._imagenp = None
    self._framenp = None
    self._reflectnp = None
    
    self.isTransparent = False

  def _createTexture(self):
    if not self._texture:
      print "."
      self._image = self.loadImage(self.url)
      self._image.addAlpha()
      
      self._texture = Texture()
      self._texture.load(self._image)
    

  def _createPanel(self):
    self._createTexture()
    
    ''' Building the photo image panel. '''
    cm=CardMaker('')
    self._imagenp = self.node_path.attachNewNode(cm.generate())
    self._framenp = self.node_path.attachNewNode(cm.generate())
    
    ratio = float(self._image.getXSize()) / float(self._image.getYSize())

    if ratio > 1:
      width, height = (MAX_PICTURE_SIZE, MAX_PICTURE_SIZE / ratio)
    else:
      width, height = (MAX_PICTURE_SIZE * ratio, MAX_PICTURE_SIZE)
    
    self._imagenp.setScale(Vec3(width, 1, height))
    center = self._imagenp.getBounds().getCenter()
    self._imagenp.setPos(-center.getX(), -center.getY(), -center.getZ())
    self._imagenp.setTexture(self._texture)
    self._imagenp.setTwoSided(True)
    
    
    self._reflectnp = self._imagenp.copyTo(self.node_path)
    self._reflectnp.setZ(self._reflectnp.getZ()-1)
    self._reflectnp.setHpr(0,180,0)
    self._reflectnp.setScale(width,1,height*0.7)
    self._reflectnp.setTransparency(TransparencyAttrib.MAlpha)
    self._reflectnp.hide()
    
    
    self._framenp.setScale(Vec3(width*1.05, 1, height*1.05))
    center = self._framenp.getBounds().getCenter()
    self._framenp.setPos(-center.getX(), -center.getY()+0.05, -center.getZ())
    self._framenp.setTexture(loader.loadTexture('models/picture_frame.png'))
    self._framenp.setTwoSided(True)
    self._framenp.hide()
    

  def isLoaded(self):
    return not self._imagenp is None

  def load(self):
    if not self.isLoaded():
      self._createPanel()
    else:
      print("picture already loaded, ignoring!")
      
  def toggleGlow(self):
    if self._framenp.isHidden():
      self._framenp.show()
    else:
      self._framenp.hide()
  
  
  def toggleReflect(self):
    if self._reflectnp.isHidden():
      self.createTransparency()
      self._reflectnp.show()
    else:
      self._reflectnp.hide()
  
  def createTransparency(self):
    if self.isTransparent:
      return
    
    aMin = 0.0
    aMax = 0.5
    for y in range(0,self._image.getYSize()):
      perc = float(y)/float(self._image.getYSize())
      a = aMin + (perc*(aMax-aMin))
      for x in range(0, self._image.getXSize()):
        self._image.setAlpha(x,y,a)
    
    texture = Texture()
    texture.load(self._image)
    self._reflectnp.clearTexture()
    self._reflectnp.setTexture(texture)
    self.isTransparent = True
  


class Pr3daSet(Pr3daNodePath):
  def __init__(self):
    Pr3daNodePath.__init__(self, 'set')
    self.pictures = set()
    self._active = False
    self._layout = None

  def addPicture(self, picture):
    if not picture in self.pictures:
      self.pictures.add(picture)
    else:
      print("picture already exists in set, skipping!")

  def getPictures(self):
    return self.pictures
    
  def setLayout(self, layout):
    self._layout = layout
  
  def getLayout(self):
    return self._layout
  
  def destroy(self):
    print('destroy is not implemented!!!')

  def reparentPictures(self, wrt=False):
    for picture in self.pictures:
      np = picture.getNodePath()
      if wrt:
        np.wrtReparentTo(self.node_path)
      else:
        np.reparentTo(self.node_path)

  def display(self):
    for picture in self.pictures:
      picture.load()

  def isActive(self):
    return self._active

  def activate(self, wrt=False):
    self._active = True
    self.reparentPictures(wrt)
    self.display()

  def deactivate(self):
    self._active = False
    
  def toggleGlow(self):
    for picture in self.pictures:
      picture.toggleGlow()
  
  def toggleReflect(self):
    for picture in self.pictures:
      picture.toggleReflect()